﻿Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Windows.Controls.Primitives

Namespace Views

    Partial Public Class ViewDataModelEditor
        Public Sub New()
            MyBase.New()

            Me.InitializeComponent()

            ' Insert code required on object creation below this point.
        End Sub

        'Private Sub Thumb_DragDelta(ByVal sender As System.Object, ByVal e As System.Windows.Controls.Primitives.DragDeltaEventArgs)
        '    Dim t = TryCast(sender, Thumb)
        '    If t Is Nothing Then Return

        '    Dim li = TryCast(ListBox.ContainerFromElement(modelLst, t), ListBoxItem)
        '    If li Is Nothing Then Return

        '    li.Margin = New Thickness(
        '        li.Margin.Left + e.HorizontalChange, _
        '        li.Margin.Top + e.VerticalChange, _
        '        li.Margin.Right, _
        '        li.Margin.Bottom)


        'End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
            SetupSAS()

            Dim cstring = _
                "Provider=sas.IOMProvider.9.2;" & _
                "Data Source=_LOCAL_;SAS Workspace ID=" & ViewDataModelEditor.Workspace.UniqueIdentifier

            Dim con = New OleDb.OleDbConnection( _
                      cstring)

            Dim cmd = New OleDb.OleDbCommand()
            cmd.CommandText = sqlTxt.Text
            cmd.Connection = con

            Dim dd As New DataTable()
            Dim adp = New OleDb.OleDbDataAdapter(cmd)

            con.Open()
            adp.Fill(dd)
            con.Close()

            dd.TableName = "temp_table"
            dd.WriteXml(DataContext.Title & Now.ToString("[dd-MM-yyyy hh-mm-ss]") & ".xml")
            dgrid.ItemsSource = dd.DefaultView

            If Services.DynamicAdoProvider.Tables.ContainsKey(DataContext.Title) Then
                Services.DynamicAdoProvider.Tables(DataContext.Title) = dd
            Else
                Services.DynamicAdoProvider.Tables.Add(DataContext.Title, dd)
            End If
        End Sub

        Friend Shared WorkspaceManager As SASWorkspaceManager.WorkspaceManager
        Friend Shared Workspace As SAS.Workspace
        Private Shared Sub SetupSAS()
            If ViewDataModelEditor.WorkspaceManager Is Nothing Then
                ViewDataModelEditor.WorkspaceManager = New SASWorkspaceManager.WorkspaceManager()

                Workspace = ViewDataModelEditor.WorkspaceManager.Workspaces.CreateWorkspaceByLogicalName( _
                         "ICHOU", _
                         SASWorkspaceManager.Visibility.VisibilityProcess, _
                         "", "", "")

                Workspace.DataService.AssignLibref( _
                      "ICHOU", _
                      "", _
                      ModuleIndicateurs.currentDataConfig.Folder, _
                      "")
            End If
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
            SetupSAS()

            Dim cstring = _
                "Provider=sas.IOMProvider.9.2;" & _
                "Data Source=_LOCAL_;SAS Workspace ID=" & Workspace.UniqueIdentifier

            Dim con = New OleDb.OleDbConnection( _
                      cstring)

            con.Open()

            Dim vv = New ViewQueryGenerator(con, "ICHOU", DataContext)

            con.Close()
            con.Dispose()
            con = Nothing

        End Sub

        Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            Dim thdata = New With {.QueryString = ""}
            Dim thdatabinding = New Binding("QueryString") With {.Mode = BindingMode.TwoWay, .Source = thdata}

            Dim th = New Thumb() With {.Style = Resources("visualPartThumb")}

            AddHandler th.DragDelta, Sub(s As Object, arg As DragDeltaEventArgs)
                                         Dim l = Canvas.GetLeft(s)
                                         If Double.IsNaN(l) Then l = 0.0
                                         Dim t = Canvas.GetTop(s)
                                         If Double.IsNaN(t) Then t = 0.0
                                         Canvas.SetLeft(s, l + arg.HorizontalChange)
                                         Canvas.SetTop(s, t + arg.VerticalChange)
                                     End Sub
            AddHandler th.MouseDoubleClick, Sub()
                                                Dim ui = ModuleIndicateurs.AppInstance.GetService(Of Common.UI.IUIService)()
                                                Dim txt = New TextBox() With {.AcceptsReturn = True, .AcceptsTab = True}
                                                BindingOperations.SetBinding(txt, TextBox.TextProperty, thdatabinding)
                                                ui.ShowMessage(txt)
                                            End Sub


            Dim mpt = Mouse.GetPosition(visualCnv)
            Canvas.SetLeft(th, mpt.X)
            Canvas.SetTop(th, mpt.Y)

            visualCnv.Children.Add(th)
        End Sub

        Private Sub MenuItem_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        End Sub
    End Class

End Namespace
